package com.sky.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.sky.domain.User;
import com.sky.exception.UserException;
import com.sky.service.UserService;
import com.sky.utils.PassWordutil;

public class UpdatePwdServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数 ，用户名，密码，确认密码
		    Map<String, String[]> map = request.getParameterMap();
		    User user =new User();
		    try {
		    	//user封装了用户名和密码
				BeanUtils.populate(user, map);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    
		    UserService  userService =new UserService();
		    String confirmpassword = request.getParameter("confirmpassword");
		     //2.处理业务逻辑 ，
		       //两次密码不一致 
		    if(!confirmpassword.equals(user.getPassword())){
		    	  request.setAttribute("user", user);
		    	  request.setAttribute("error_msg", "两次密码不一致");
		    	  request.setAttribute("confirmpassword", confirmpassword);
		    	  request.getRequestDispatcher("modifypwd.jsp").forward(request, response);
		    	return;
		    }
		    
		    //判断密码格式
		    if(!user.getPassword().matches("^\\d{6,}$")){
		    	request.setAttribute("user", user);
	    		request.setAttribute("confirmpassword", confirmpassword);
	    		request.setAttribute("error_msg", "密码格式不正确");
	    		request.getRequestDispatcher("modifypwd.jsp").forward(request, response);
	    		return;
		    }
		   
			try {
				 //根据用户名查询原来的密码
				String  pwd = userService.queryPwdByUserName(user.getUsername());
				 if (pwd.equals(confirmpassword)) {
					    request.setAttribute("user", user);
			    		request.setAttribute("confirmpassword", confirmpassword);
			    		request.setAttribute("error_msg", "新密码不能和原密码一样");
			    		request.getRequestDispatcher("modifypwd.jsp").forward(request, response);
			    		return;
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		   
			try {
				try {
					//=======================
					
					String pwd2=user.getPassword();
				    try {
						PassWordutil des1 = new PassWordutil();
						user.setPassword(des1.encrypt(pwd2));
					} catch (Exception e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					
					//=========================
					
					userService.updatePwd(user);
					//修改密码成功.重新登录
					response.getWriter().write("<script type='text/javascript'>"
							+ "window.top.location.href='login.jsp'</script>");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					request.setAttribute("user", user);
		    		request.setAttribute("confirmpassword", confirmpassword);
		    		request.setAttribute("error_msg", e.getMessage());
		    		request.getRequestDispatcher("modifypwd.jsp").forward(request, response);
				}
				
			} catch (UserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				//修改密码失败
				request.setAttribute("user", user);
	    		request.setAttribute("confirmpassword", confirmpassword);
	    		request.setAttribute("error_msg", e.getMessage());
	    		request.getRequestDispatcher("modifypwd.jsp").forward(request, response);
			}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}


}
